package com.jhgsys.internal.sypq.controller;

import com.jhgsys.internal.common.annotation.Log;
import com.jhgsys.internal.common.controller.BaseController;
import com.jhgsys.internal.common.entity.FebsConstant;
import com.jhgsys.internal.common.entity.FebsResponse;
import com.jhgsys.internal.common.entity.QueryRequest;
import com.jhgsys.internal.common.entity.FebsConstant;
import com.jhgsys.internal.common.exception.FebsException;
import com.jhgsys.internal.common.utils.FebsUtil;
import com.jhgsys.internal.sypq.entity.TaskShip;
import com.jhgsys.internal.sypq.service.ITaskShipService;
import com.wuwenze.poi.ExcelKit;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;
import javax.validation.constraints.NotBlank;
import java.util.Date;

/**
 * 任务所需船模 Controller
 *
 * @author ganjun
 * @date 2024-11-18 10:23:27
 */
@Slf4j
@Validated
@Controller
public class TaskShipController extends BaseController {

    @Autowired
    private ITaskShipService taskShipService;

    @GetMapping(FebsConstant.VIEW_PREFIX + "taskShip")
    @RequiresPermissions("taskShip:view")
    public String taskShipIndex(){
        return jumpPage("sypq/taskShip/taskShipList");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "taskShip/add")
    @RequiresPermissions("taskShip:add")
    public String taskShipAdd(){
        return jumpPage("sypq/taskShip/taskShipEdit");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "taskShip/update/{id}")
    @RequiresPermissions("taskShip:edit")
    public String taskShipUpdate(Model model, @PathVariable("id") String id){
        model.addAttribute("taskShip", taskShipService.findTaskShipById(id));
        return jumpPage("sypq/taskShip/taskShipEdit");
    }

    /**
     * 查询（所有）
     * @author ganjun
     * @date 2024-11-18 10:23:27
     */
    @GetMapping("taskShip")
    @ResponseBody
    @RequiresPermissions("taskShip:view")
    public FebsResponse getAllTaskShips(TaskShip taskShip) {
        try {
            return success(taskShipService.findTaskShipList(taskShip));
        }catch (Exception ex){
            return failure(ex);
        }

    }

    /**
     * 查询（分页）
     * @author ganjun
     * @date 2024-11-18 10:23:27
     */
    @GetMapping("taskShip/list")
    @ResponseBody
    @RequiresPermissions("taskShip:view")
    public FebsResponse taskShipList(QueryRequest request, TaskShip taskShip) {
        try {
            Map<String, Object> dataTable = getDataTable(this.taskShipService.findTaskShipPage(request, taskShip));
            return success(dataTable);
        }catch (Exception ex){
            return failure(ex);
        }
    }

    /**
     * 新增 TaskShip
     * @author ganjun
     * @date 2024-11-18 10:23:27
     */
    @Log("新增TaskShip")
    @PostMapping("taskShip")
    @ResponseBody
    @RequiresPermissions("taskShip:add")
    public FebsResponse addTaskShip(@Valid TaskShip taskShip) throws FebsException {
        try {
            this.taskShipService.createTaskShip(taskShip);
            return success();
        } catch (Exception ex) {
            return failure(ex,"新增失败");
        }
    }

    /**
     * 逻辑删除 TaskShip
     * @author ganjun
     * @date 2024-11-18 10:23:27
     */
    @Log("逻辑删除TaskShip")
    @GetMapping("taskShip/delete/{id}")
    @ResponseBody
    @RequiresPermissions("taskShip:delete")
    public FebsResponse deleteTaskShip(@PathVariable("id") String taskShipIds) throws FebsException {
        try {
            TaskShip taskShip = new TaskShip();
            taskShip.setDeletedBy(getCurrentUser().getUserName());
            taskShip.setDeletedTime(new Date());
            taskShip.setDeletedFlg(FebsConstant.INVALID);
            this.taskShipService.deleteTaskShip(taskShip,taskShipIds);
            return success();
        } catch (Exception ex) {
            return failure(ex,"删除失败");
        }
    }

    /**
    * 物理删除 TaskShip
    * @author ganjun
    * @date 2024-11-18 10:23:27
    */
    @Log("物理删除TaskShip")
    @GetMapping("taskShip/physicallyDelete/{id}")
    @ResponseBody
    @RequiresPermissions("taskShip:delete")
    public FebsResponse physicallyDeleteTaskShip(@PathVariable("id") String taskShipIds) throws FebsException {
        try {
            this.taskShipService.physicallyDeleteTaskShip(taskShipIds);
            return success();
        } catch (Exception ex) {
            return failure(ex,"删除失败");
        }
    }

    /**
     * 修改 TaskShip
     * @author ganjun
     * @date 2024-11-18 10:23:27
     */
    @Log("修改TaskShip")
    @PostMapping("taskShip/update")
    @ResponseBody
    @RequiresPermissions("taskShip:edit")
    public FebsResponse updateTaskShip(TaskShip taskShip) throws FebsException {
        try {
            this.taskShipService.updateTaskShip(taskShip);
            return success();
        } catch (Exception ex) {
            return failure(ex,"修改失败");
        }
    }

}
